home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / inkscape / extensions / embedimage.py < prev    next >
Encoding:
Python Source  |  2010-03-12  |  4.3 KB  |  109 lines

  1. #!/usr/bin/env python 
  2. '''
  3. Copyright (C) 2005,2007 Aaron Spike, aaron@ekips.org
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. '''
  19.  
  20. import inkex, os, base64, urlparse, urllib
  21. import gettext
  22. _ = gettext.gettext
  23.  
  24. class Embedder(inkex.Effect):
  25.     def __init__(self):
  26.         inkex.Effect.__init__(self)
  27.         self.OptionParser.add_option("-s", "--selectedonly",
  28.             action="store", type="inkbool", 
  29.             dest="selectedonly", default=False,
  30.             help="embed only selected images")
  31.  
  32.     def effect(self):
  33.         # if slectedonly is enabled and there is a selection only embed selected
  34.         # images. otherwise embed all images
  35.         if (self.options.selectedonly):
  36.             self.embedSelected(self.document, self.selected)
  37.         else:
  38.             self.embedAll(self.document)
  39.  
  40.     def embedSelected(self, document, selected):
  41.         self.document=document
  42.         self.selected=selected
  43.         if (self.options.ids):
  44.             for id, node in selected.iteritems():
  45.                 if node.tag == inkex.addNS('image','svg'):
  46.                     self.embedImage(node)
  47.  
  48.     def embedAll(self, document):
  49.         self.document=document #not that nice... oh well
  50.         path = '//svg:image'
  51.         for node in self.document.getroot().xpath(path, namespaces=inkex.NSS):
  52.             self.embedImage(node)
  53.  
  54.     def embedImage(self, node):
  55.         xlink = node.get(inkex.addNS('href','xlink'))
  56.         if xlink is None or xlink[:5] != 'data:':
  57.             absref=node.get(inkex.addNS('absref','sodipodi'))
  58.             url=urlparse.urlparse(xlink)
  59.             href=urllib.unquote(url.path)
  60.             if os.name == 'nt' and href[0] == '/':
  61.                 href = href[1:]
  62.             path=''
  63.             #path selection strategy:
  64.             # 1. href if absolute
  65.             # 2. realpath-ified href
  66.             # 3. absref, only if the above does not point to a file
  67.             if (href != None):
  68.                 path=os.path.realpath(href)
  69.             if (not os.path.isfile(path)):
  70.                 if (absref != None):
  71.                     path=absref
  72.  
  73.             if (not os.path.isfile(path)):
  74.                 inkex.errormsg(_('No xlink:href or sodipodi:absref attributes found, or they do not point to an existing file! Unable to embed image.'))
  75.                 if path:
  76.                     inkex.errormsg(_("Sorry we could not locate %s") % path)
  77.  
  78.             if (os.path.isfile(path)):
  79.                 file = open(path,"rb").read()
  80.                 embed=True
  81.                 if (file[:4]=='\x89PNG'):
  82.                     type='image/png'
  83.                 elif (file[:2]=='\xff\xd8'):
  84.                     type='image/jpeg'
  85.                 elif (file[:2]=='BM'):
  86.                     type='image/bmp'
  87.                 elif (file[:6]=='GIF87a' or file[:6]=='GIF89a'):
  88.                     type='image/gif'
  89.                 elif (file[:4]=='MM\x00\x2a' or file[:4]=='II\x2a\x00'):
  90.                     type='image/tiff'
  91.                 #ico files lack any magic... therefore we check the filename instead
  92.                 elif(path.endswith('.ico')):
  93.                     type='image/x-icon' #official IANA registered MIME is 'image/vnd.microsoft.icon' tho
  94.                 else:
  95.                     embed=False
  96.                 if (embed):
  97.                     node.set(inkex.addNS('href','xlink'), 'data:%s;base64,%s' % (type, base64.encodestring(file)))
  98.                     if (absref != None):
  99.                         del node.attrib[inkex.addNS('absref',u'sodipodi')]
  100.                 else:
  101.                     inkex.errormsg(_("%s is not of type image/png, image/jpeg, image/bmp, image/gif, image/tiff, or image/x-icon") % path)
  102.  
  103. if __name__ == '__main__':
  104.     e = Embedder()
  105.     e.affect()
  106.  
  107.  
  108. # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99
  109.